package com.hdyg.sixg.activity.mine;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.hdyg.sixg.R;
import com.hdyg.sixg.activity.common.BaseActivity;
import com.hdyg.sixg.entry.InfoBean;
import com.hdyg.sixg.mvp.Contrant.CCompleteInfo;
import com.hdyg.sixg.mvp.base.BaseUrl;
import com.hdyg.sixg.mvp.base.GetParamUtil;
import com.hdyg.sixg.mvp.presenter.PCompleteInfo;
import com.hdyg.sixg.util.StringUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import butterknife.BindView;

/**
 * @author CZB
 * @describe 完善资料
 * @time 2019/4/8 15:44
 */
public class CompleteInfoActivity extends BaseActivity implements CCompleteInfo.IVCompleteInfo {

    @BindView(R.id.ll_top_left)
    LinearLayout llTopLeft;
    @BindView(R.id.tvTopTitle)
    TextView tvTopTitle;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_bank_name)
    EditText etBankName;
    @BindView(R.id.et_area)
    EditText etArea;
    @BindView(R.id.et_bank_branch)
    EditText etBankBranch;
    @BindView(R.id.et_bank_number)
    EditText etBankNumber;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_alipay)
    EditText etAlipay;
    @BindView(R.id.bt_bind)
    Button btBind;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout srlRefresh;

    private CCompleteInfo.IPCompleteInfo mPresenter;
    private String bankname,bank_open,bankadd,bank_branch,bankcard,bankphone,alipay;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_complete_info;
    }

    @Override
    protected void initView() {
        onClickListener();
        tvTopTitle.setText(R.string.info_title);
    }
    private void onClickListener(){
        srlRefresh.setOnRefreshListener(refreshLayout -> {
            srlRefresh.finishRefresh(2000);//传入false表示刷新失败
            initData();
        });
        llTopLeft.setOnClickListener(v -> finish());
        //绑定
        btBind.setOnClickListener(v -> {
            bankname = etName.getText().toString().trim();
            bank_open = etBankName.getText().toString().trim();
            bankadd = etArea.getText().toString().trim();
            bank_branch = etBankBranch.getText().toString().trim();
            bankcard = etBankNumber.getText().toString().trim();
            bankphone = etPhone.getText().toString().trim();
            alipay = etAlipay.getText().toString().trim();
            if (TextUtils.isEmpty(bankname)){
                toastNotifyShort(R.string.info_name_hint);
                return;
            }
            if (TextUtils.isEmpty(bank_open)){
                toastNotifyShort(R.string.info_bank_name_hint);
                return;
            }
            if (TextUtils.isEmpty(bankadd)){
                toastNotifyShort(R.string.info_area_hint);
                return;
            }
            if (TextUtils.isEmpty(bank_branch)){
                toastNotifyShort(R.string.info_bank_branch_hint);
                return;
            }
            if (TextUtils.isEmpty(bankcard)){
                toastNotifyShort(R.string.info_bank_number_hint);
                return;
            }
            if (TextUtils.isEmpty(bankphone)){
                toastNotifyShort(R.string.info_phone_hint);
                return;
            }
            if (TextUtils.isEmpty(alipay)){
                toastNotifyShort(R.string.info_alipay_hint);
                return;
            }
            if (StringUtil.isFastDoubleClick()){
                return;
            }
            mPresenter.pGetBindInfo(BaseUrl.PERFET_INFO_URL,
                    GetParamUtil.getCompleteInfoParam(bankname,bank_open,bankadd,bank_branch,bankcard,bankphone,alipay));
        });
    }

    @Override
    protected void initData() {
        mPresenter.pGetInit(BaseUrl.PERSON_INFO_URL,GetParamUtil.getMineInfoParam());
    }

    @Override
    protected void createPresenter() {
        mPresenter = new PCompleteInfo(this);
    }
    //设置是否可编辑
    private void setViewFocusable(boolean flag,InfoBean dataBean){
        if (flag){
            //可编辑
            btBind.setVisibility(View.VISIBLE);
        }else {
            //不可编辑
            btBind.setVisibility(View.GONE);
        }
        if (dataBean.getDetail() == null){
            return;
        }
        //设置文本
        etName.setText(dataBean.getDetail().getBankname());
        etBankName.setText(dataBean.getDetail().getBank_open());
        etArea.setText(dataBean.getDetail().getBankadd());
        etBankBranch.setText(dataBean.getDetail().getBank_branch());
        etBankNumber.setText(dataBean.getDetail().getBankcard());
        etPhone.setText(dataBean.getDetail().getBankphone());
        etAlipay.setText(dataBean.getDetail().getAlipay());
        //设置是否可编辑
        etName.setFocusable(flag);
        etName.setFocusableInTouchMode(flag);
        etBankName.setFocusable(flag);
        etBankName.setFocusableInTouchMode(flag);
        etArea.setFocusable(flag);
        etArea.setFocusableInTouchMode(flag);
        etBankBranch.setFocusable(flag);
        etBankBranch.setFocusableInTouchMode(flag);
        etBankNumber.setFocusable(flag);
        etBankNumber.setFocusableInTouchMode(flag);
        etPhone.setFocusable(flag);
        etPhone.setFocusableInTouchMode(flag);
        etAlipay.setFocusable(flag);
        etAlipay.setFocusableInTouchMode(flag);
    }

    @Override
    public void vGetInitSuccess(InfoBean dataBean) {
        if ("1".equals(dataBean.getIs_bind())){
            setViewFocusable(false,dataBean);
        }else {
            setViewFocusable(true,dataBean);
        }
    }

    @Override
    public void vGetBindInfoSuccess(String dataBean) {
        toastNotifyShort(dataBean);
        finish();
    }
}
